/*
package com.utils.spring;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.kyhd.fdp.controller.Model.CustomerDetailResponse;
import com.kyhd.fdp.controller.Model.CustomerItem;
import com.kyhd.fdp.controller.Model.ListCustomerResponse;
import com.kyhd.fdp.controller.Model.CustomerItem.CustomerPlatformConsumer;
import com.kyhd.fdp.dao.CustomerRepository;
import com.kyhd.fdp.dao.model.Customer;
import com.kyhd.fdp.util.MathUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CustomerService {
	
	@Autowired
	CustomerRepository customerRepository;
	
	@Autowired
	RebateRatioService rebateRatioService;
	
	@Autowired
	@Qualifier("entityManager")
	EntityManager entityManager;
	
    public Customer getById(long id){
        return this.customerRepository.getById(id);
    }
    
	public Customer getByCustomerName(String customerName) {
		return this.customerRepository.getByCustomerName(customerName);
	}
	
	public Customer getByCustomerCode(String customerCode) {
		return this.customerRepository.getByCustomerCode(customerCode);
	}

    public void update(Customer customer) {
    	customer.setLtime(DateTime.now());
        this.customerRepository.save(customer);
    }
   
	public List<Customer> getCustomerList() {
		return this.customerRepository.getCustomerList();
	}
	
	public Customer save(Customer customer) {
        customer =  this.customerRepository.save(customer);
        DecimalFormat df=new DecimalFormat("000");
        customer.setCustomerCode("E"+df.format(customer.getId()));
        return this.customerRepository.save(customer);
	}

	@SuppressWarnings("unchecked")
	public ListCustomerResponse searchCustomer(Date sdate, Date edate, 
			final String customerCode, int offset, int limit) {
		
		List<CustomerItem> ctList = new ArrayList<CustomerItem>();
		StringBuilder sql = new StringBuilder("select Date(a.date),a.cid,a.customer_code,a.customer_name,	");
		sql.append(" IFNULL(MAX(CASE a.name WHEN  'wifi万能钥匙' THEN IFNULL(a.sc,0) END),0) wifi ,"); 
		sql.append(" IFNULL(MAX(CASE a.name WHEN '猎豹' THEN IFNULL(a.sc,0) END) ,0) 猎豹,"); 
		sql.append(" IFNULL(MAX(CASE a.name WHEN  '趣头条' THEN IFNULL(a.sc,0) END) ,0) 趣头条,"); 
		sql.append(" (IFNULL(MAX(CASE a.name WHEN 'wifi万能钥匙' THEN IFNULL(a.sc,0) END),0)+");
		sql.append(" IFNULL(MAX(CASE a.name WHEN '猎豹' THEN IFNULL(a.sc,0) END) ,0)+"); 
		sql.append(" IFNULL(MAX(CASE a.name WHEN '趣头条' THEN IFNULL(a.sc,0) END) ,0) ) tconsume "); 
		sql.append(" from (select IFNULL(Date(ad.importDate),date_sub(curdate(), interval 1 day)) date ,cs.id cid,cs.customer_code,cs.customer_name, "); 
		sql.append(" ad.platform_id pid,pf.name,sum(IFNULL(ad.consume,0)) sc "); 
		sql.append(" from customer cs left join account_data ad on ad.customer_id = cs.id "); 
		sql.append(" left join platform pf on ad.platform_id = pf.id where 1=1 ");
		if(sdate!=null&&edate!=null) {
			sql.append(" and ( ( Date(ad.importDate) >=?1 and Date(ad.importDate)<=?2  ) or ad.importDate is null) ");	
		}
		if(customerCode!=null&&!"".equals(customerCode.trim())) {
			sql.append(" and cs.customer_code=?3 ");
		}
		sql.append(" and cs.deleted=0 and (ad.deleted=0 or ad.deleted is null) and (pf.deleted =0 or pf.deleted  is null) ");
		sql.append(" group by ad.importDate ,cs.id,ad.platform_id ) a group by a.date ,a.cid order by a.date desc ,a.cid desc ");
		
		ListCustomerResponse response = new ListCustomerResponse();
		List<Object[]> list = null;
		Object count = null;
		
		String sqlLimit = "select lt.* from ( "
				+sql.toString()
				+" ) lt limit "
				+(offset-1)*limit+", "+limit+" ";
		
		String sqlCount = "select count(1) from ("+sql.toString()+") acu";
				
		try {
			Query queryCount = this.entityManager.createNativeQuery(sqlCount.toString());
			if(sdate!=null&&edate!=null) {
				queryCount.setParameter(1, sdate);
				queryCount.setParameter(2, edate);				
			}
			if(customerCode!=null&&!"".equals(customerCode.trim())) {
				queryCount.setParameter(3, customerCode);
			}
			count = queryCount.getSingleResult();
			
			Query query = this.entityManager.createNativeQuery(sqlLimit.toString());
			if(sdate!=null&&edate!=null) {
				query.setParameter(1, sdate);
				query.setParameter(2, edate);				
			}
			if(customerCode!=null&&!"".equals(customerCode.trim())) {
				query.setParameter(3, customerCode);
			}

			list = query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return response;
		}
		for (Object[] q : list) {
			CustomerItem ct = new CustomerItem();
			ct.setTime(q[0].toString().replace("-", "")); // 日期转换格式
			ct.setId(Long.valueOf(q[1].toString()));
			ct.setCustomerCode(q[2].toString());
			ct.setCustomerName(q[3].toString());

			List<CustomerPlatformConsumer> listConsume = new ArrayList<CustomerPlatformConsumer>();
			CustomerPlatformConsumer cpc1 = new CustomerPlatformConsumer();
			CustomerPlatformConsumer cpc2 = new CustomerPlatformConsumer();
			CustomerPlatformConsumer cpc3 = new CustomerPlatformConsumer();
			cpc1.setPlatformName("wifi");
			cpc1.setTodayConsumer(MathUtil.decimalFormatString(q[4].toString()));
			listConsume.add(cpc1);
			cpc2.setPlatformName("猎豹");
			cpc2.setTodayConsumer(MathUtil.decimalFormatString(q[5].toString()));
			listConsume.add(cpc2);
			cpc3.setPlatformName("趣头条");
			cpc3.setTodayConsumer(MathUtil.decimalFormatString(q[6].toString()));
			listConsume.add(cpc3);

			ct.setListConsume(listConsume);

			ct.setTotalConsume(MathUtil.decimalFormatString(q[7].toString()));
			ctList.add(ct);

			log.info("CustomerItem:{}", ct);
		}
		
		response.setItems(ctList);
		response.setTotalNums(Long.valueOf(count.toString()));
		
        return response;
	}

	@SuppressWarnings("unchecked")
	public List<CustomerDetailResponse> searchCustomerDetail(Date time, Long id) {

		StringBuilder sql = new StringBuilder(" select Date(a.importDate),a.product_id,pd.name pdname,a.platform_id ,pf.name pfname,IFNULL(sum(a.consume),0) ");
		sql.append(" from account_data a ");
		sql.append(" LEFT JOIN product pd on a.product_id = pd.id ");
		sql.append(" LEFT JOIN platform pf on a.platform_id = pf.id ");
		sql.append(" where Date(a.importDate)=?1 and a.customer_id=?2 ");
		sql.append(" and a.deleted=0 and pd.deleted=0 and pf.deleted=0 ");
		sql.append(" group by a.platform_id ,a.product_id order by  a.product_id ");
		
		List<CustomerDetailResponse> rList = new ArrayList<CustomerDetailResponse>();
		List<Object[]> list = null;

		try {

			Query query = this.entityManager.createNativeQuery(sql.toString());
			query.setParameter(1, time);
			query.setParameter(2, id);
			list = query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return rList;
		}

		for (Object[] q : list) {
			CustomerDetailResponse customerDetail = new CustomerDetailResponse();
			customerDetail.setTime(q[0].toString().replace("-", ""));
			customerDetail.setProductId(Long.valueOf(q[1].toString()));
			customerDetail.setProductName(q[2].toString());
			customerDetail.setPlatformId(Long.valueOf(q[3].toString()));  
			customerDetail.setPlatformName(q[4].toString());
			customerDetail.setTotalConsume(MathUtil.decimalFormatString(q[5].toString()));
			
			Double reBateRatio = this.rebateRatioService
					.findValueByProductIdAndToday(q[0].toString() , customerDetail.getProductId());
			
			customerDetail.setDiscdConsume(MathUtil.decimalFormat(MathUtil.div(Double.valueOf(q[5].toString()), 1+reBateRatio)));
		
			rList.add(customerDetail);
		}
		
		return rList;
	}

	public void updateProduct(long ct_id, long pf_id, String v2) {
		this.customerRepository.updateProduct(ct_id,pf_id,v2);
	}
		


	public void updateAccount_data(long ct_id, long pf_id, long pd_id) {
		this.customerRepository.updateAccount_data(ct_id,pf_id,pd_id);

	}

	public void updateAccount_data(long pro_id, long acc_id) {
		this.customerRepository.updateAccount_data(pro_id,acc_id);
	}

	public void updateProduct_with_account(long pro_id, long acc_id) {
		this.customerRepository.updateProduct_with_account(pro_id,acc_id);
	}

}
*/